//
//  MyAccountVC.swift
//  ShareUmbrella
//
//  Created by niupark on 2017/8/12.
//  Copyright © 2017年 尧尚信息科技. All rights reserved.
//

import UIKit
import YSKit

class MyAccountVC: YSBaseViewController,UITableViewDelegate,UITableViewDataSource{
    let kScreenHeight = UIScreen.main.bounds.size.height
    let kScreenWidth = UIScreen.main.bounds.size.width
    var selfTableView:UITableView!
    var dataDic = NSDictionary.init()
    
    let dataArr = [["帮助评价","谁评价我"],["分享次数","分享里程","分享时间"],["我的积分"]]
    
    override func setup() {
        super.setup()
    }
    
    /*
     *
     * 界面布局初始化
     */
    override func buildUI() {
        super.buildUI()
        self.title = I18n("VC.LFSide.account.titlte")
        
        self.edgesForExtendedLayout = UIRectEdge(rawValue: 0)
        
        self.initBackgourndViewColor()
        self.addSelfTableView()
        self.getUserInfo()
        
        
    }
    
    //
    override func buildRx() {
        super.buildRx()
    }
    
    //RGB
    func RGB(r:CGFloat,_ g:CGFloat,_ b:CGFloat) -> UIColor{
        return UIColor(red: (r)/255.0, green: (g)/255.0, blue: (b)/255.0, alpha: 1.0)
    }
    
    //初始化背景颜色
    func initBackgourndViewColor() {
        self.view.backgroundColor = RGB(r: 239, 239, 243)
    }
    
    
    //用户信息
    func getUserInfo() -> Void {
        _ = YSNetWorkManager.provider.requestJSON(YSStructTarget(UserAPI.userInfo(token: Auth.token))).subscribe(onNext: { [weak self] (data) in
            //
            HUD.hide()
            guard let _ = self else {
                return
            }
            
            guard let userInfo = data else{
                return
            }
            
            self?.dataDic = data!["data"].dictionaryObject! as NSDictionary
            
            UserService.sharedInstance.km.value = userInfo["data"]["shareMile"].stringValue
            UserService.sharedInstance.times.value = userInfo["data"]["shareTime"].stringValue
            //UserService.sharedInstance.point.value = userInfo["data"]["score"].string ?? "0"
            UserService.sharedInstance.count.value = userInfo["data"]["shareCount"].stringValue
            
            self?.selfTableView.reloadData()
            
            }, onError: { [weak self](ErrorType) in
                HUD.hide()
                guard let  `self` = self else {
                    return
                }
                self.handle(withError: ErrorType)
        })
        
    }
    
    //添加一个tableview
    func addSelfTableView(){
        
        selfTableView = UITableView.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight), style: UITableViewStyle.plain)
        selfTableView.dataSource = self
        selfTableView.delegate = self
        selfTableView.backgroundColor = UIColor.clear
        selfTableView.separatorStyle = UITableViewCellSeparatorStyle.none;
        selfTableView.showsHorizontalScrollIndicator = false
        selfTableView.showsVerticalScrollIndicator = false
        self.view.addSubview(selfTableView)
    }
    
    //MARK:UITableView DataSource
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataArr.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr[section].count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let iderntify:String = "swiftCell"
        var cell = tableView.dequeueReusableCell(withIdentifier: iderntify) as? MineCell
        if(cell == nil){
            cell=MineCell(style: UITableViewCellStyle.default
                , reuseIdentifier: iderntify);
            
        }
        let row = indexPath.row
        let section = indexPath.section
        
        let str = dataArr[section][row]
        
        cell?.titleLabel?.text = str
        
        if(str == "求助消息" || str == "分享时间"  || str == "我的积分" ){
            cell?.line?.alpha = 0.0
        }
        
        if(str == "分享次数"){
            let s = dataDic["shareCount"] as? String
            let ss = s ?? "0"
            cell?.valueLeb?.text = ss
            cell?.nextIcon?.alpha = 0.0
        }else if(str == "分享里程"){
            let s = dataDic["shareMile"] as? String
            let ss = s ?? "0"
            cell?.valueLeb?.text = ss + "km"
            cell?.nextIcon?.alpha = 0.0
        }else if(str == "分享时间"){
            let s = dataDic["shareTime"] as? String
            let ss = s ?? "0"
            cell?.valueLeb?.text = ss + "min"
            cell?.nextIcon?.alpha = 0.0
        }
        
        return cell!
    }
    
    //MARK:UITableView Delegate
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selfTableView?.deselectRow(at: indexPath, animated: true)
        
        let section = indexPath.section
        let row = indexPath.row
        let str = dataArr[section][row]
        
        if(str == "帮助评价"){
            self.ys.push(CommentHelpVC())
        }else if(str == "谁评价我"){
            self.ys.push(CommentSeekVC())
        }else if(str == "我的积分"){
            self.ys.push(IntegralHistoryVC())
        }
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 20))
        if(section == 0){
            view.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: 0)
        }
        return view
    }
}
